package com.ocom.webapi.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelReader;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.ocom.common.constant.WebApiServiceUrl;
import com.ocom.common.dto.Department;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.payconfig.GetOrderForecastDetailRequest;
import com.ocom.common.request.payconfig.UpdateOrderForecastDetailRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.utils.poi.ExcelUtil;
import com.ocom.webapi.feign.HumanClientService;
import com.ocom.webapi.feign.MqSimpleService;
import com.ocom.webapi.feign.PayConfigClientService;
import com.ocom.webapi.vo.ForecastDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@PreAuthorize("@pms.hasRole('ROLE_WEB')" )
public class ForecastDataController {
    @Autowired
    private HumanClientService humanClientService;

    @Autowired
    private PayConfigClientService payConfigClientService;

    @Autowired
    private MqSimpleService simpleService;

    @PostMapping(WebApiServiceUrl.URL_OrderForecastDetail_onShow)
    public Result orderForecastDetailOnShow(@RequestBody GetOrderForecastDetailRequest request){
        if(ObjectUtil.isNotEmpty(request.getDeptId()) && request.getDeptId() > 0){
            Department department = new Department();
            department.setComId(request.getComId());
            department.setDeptId(Long.valueOf(request.getDeptId()));
            department.setShowLevel(1);
            department.setPage(-1);
            department.setSize(-1);
            Result resultA = this.humanClientService.getDeptIds(department);
            if(resultA.getCode() == 200){
                String data = JSON.toJSONString(resultA.getData());
                JSONObject jsonObject = JSON.parseObject(data);
                List<String> deptIds = Lists.newArrayList(String.valueOf(request.getDeptId()));
                if(StrUtil.isNotEmpty(jsonObject.getString("deptIds"))){
                    deptIds.add(jsonObject.getString("deptIds"));
                }
                request.setDeptIds(StrUtil.join(",",deptIds));
            }
        }
        return this.payConfigClientService.getOrderForecastDetail(request);
    }

    @PostMapping(WebApiServiceUrl.URL_OrderForecastDetail_importData)
    public Result orderForecastDetailImportData(Long comId,
                                                MultipartFile file,
                                                Integer pointId,
                                                String dateList,
                                                String mealList,
                                                Integer number,
                                                Integer allowDays,
                                                Integer allowTime,
                                                Integer overdraw){
        if(file.getSize() == 0){
            return ResultUtil.error(ResultEnum.Err_3058);
        }
        String filePath = file.getOriginalFilename();
        String filePaths = filePath.substring(filePath.length() - 4).toLowerCase();
        if (!filePaths.equals(".xls") & !filePaths.equals("xlsx")) {
            return ResultUtil.error(ResultEnum.Err_3058);
        }
        try {
            ExcelReader reader = cn.hutool.poi.excel.ExcelUtil.getReader(file.getInputStream());
            List<List<Object>> allData = reader.read();
            for (int i = 0; i < allData.size(); i++) {
                if(i == 0){continue;}
                if(ObjectUtil.isEmpty(allData.get(i).get(0))){
                    continue;
                }
                if(ObjectUtil.isEmpty(allData.get(i).get(1))){
                    continue;
                }
                UpdateOrderForecastDetailRequest updateOrderForecastDetailRequest = new UpdateOrderForecastDetailRequest();
                updateOrderForecastDetailRequest.setAllowDays(allowDays);
                updateOrderForecastDetailRequest.setMealList(mealList);
                updateOrderForecastDetailRequest.setNumber(number);
                updateOrderForecastDetailRequest.setOverdraw(overdraw);
                updateOrderForecastDetailRequest.setAllowTime(allowTime);
                updateOrderForecastDetailRequest.setComId(comId);
                updateOrderForecastDetailRequest.setDateList(dateList);
                updateOrderForecastDetailRequest.setPointId(pointId);
                updateOrderForecastDetailRequest.setPCode(String.valueOf(allData.get(i).get(0)));
                updateOrderForecastDetailRequest.setPName(String.valueOf(allData.get(i).get(1)));
                this.simpleService.forecastData(updateOrderForecastDetailRequest);
            }
        }catch (Exception e){
            return ResultUtil.error(ResultEnum.Err_4204);
        }
        return ResultUtil.success();
    }
}
